package com.example.sample.activity;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import com.example.app.BaseActivity;
import com.example.sample.R;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import net.liteheaven.xblib.common.io.MemUtil;

public class CleanUpActivity extends BaseActivity {
	
	private TextView mTvResult;
	private Button mBtnCleanUp;
    private Button mBtnKillSelf;
    private Button mBtnExit;
    private TextView mTvMem;
    private Timer mUpdateTimer;
    private Handler mHandler;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cleanup);
		mTvResult = (TextView)findViewById(R.id.tvResult);
		mBtnCleanUp = (Button)findViewById(R.id.btnCleanUp);
		mBtnCleanUp.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				onClickCleanUp();
			}
		});
        mBtnKillSelf = (Button)findViewById(R.id.btnKillSelf);
        mBtnKillSelf.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                android.os.Process.killProcess(android.os.Process.myPid());
            }
        });
        mBtnExit = (Button)findViewById(R.id.btnExit);
        mBtnExit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                System.exit(0);
            }
        });
        mTvMem = (TextView)findViewById(R.id.tvMem);

        mHandler = new Handler();
        startTimer();
	}

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopTimer();
    }

    private void onClickCleanUp(){
		new AsyncTask<Void, Void, Void>(){
			
			private String report = "";

			@Override
			protected Void doInBackground(Void... params) {
				report = MemUtil.cleanUp(CleanUpActivity.this, new ArrayList<String>());
				return null;
			}
			
			@Override
			protected void onPostExecute(Void result) {
				mTvResult.setText(report);
				super.onPostExecute(result);
			}
			
		}.execute();
	}

    private void update(){
        long ava = MemUtil.getAvailMemory(this) / 1024 / 1024;
        long total = MemUtil.getTotalMemory() / 1024;
        int percentage = MemUtil.getAvaMemPercentage(this);
        String report = "Memory:     " + ava + "M / " + total + "M" + "  ( " +  percentage + " % )";
        mTvMem.setText(report);
    }

    private void startTimer(){
        stopTimer();
        if (mUpdateTimer == null){
            mUpdateTimer = new Timer();
            TimerTask tt = new TimerTask() {
                @Override
                public void run() {
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            update();
                        }
                    });
                }
            };
            mUpdateTimer.schedule(tt, 0, 1000);
        }
    }

    private void stopTimer(){
        if (mUpdateTimer != null){
            mUpdateTimer.cancel();
            mUpdateTimer = null;
        }
    }
}
